/**
 * axios实例
 */
import axios from 'axios';
import type { AxiosResponse } from 'axios';
import { unref } from 'vue';
import type { Action } from 'element-plus';
import { ElMessageBox } from 'element-plus/es';
import { API_BASE_URL, API_PROXY_URL, LAYOUT_PATH } from '@/config/setting';
import type { ApiResult } from '@/api';
import router from '@/router';
import { getToken, setToken } from './token-util';
import { logout, toURLSearch } from './common';

/** 创建axios实例 */
const service = axios.create({
	baseURL: API_BASE_URL //这里采用ngix转发处理跨域，API_BASE_URL = /jm
});

/**
 * 添加请求拦截器
 */
service.interceptors.request.use(
	(config) => {
		// 添加token到header
		const token = getToken();
		if (token && config.headers) {
			config.headers['Authorization'] = token;
			config.headers['WEB_TENANT_ID'] = localStorage.getItem('webTenantId')
		}
		// get请求处理数组和对象类型参数
		if (config.method === 'get' && config.params) {
			config.url = toURLSearch(config.params, config.url);
			config.params = {};
		}
		return config;
	},
	(error) => {
		return Promise.reject(error);
	}
);

/**
 * 添加响应拦截器
 */
service.interceptors.response.use(
	(res: AxiosResponse < ApiResult < unknown >> ) => {
		// 登录过期处理
		if (res.data?.code === 100000) {
			const { path, fullPath } = unref(router.currentRoute);
			if (path == LAYOUT_PATH) {
				logout(true, void 0, router.push);
			} else if (path !== '/login') {
				ElMessageBox.close();
				ElMessageBox.alert('登录状态已过期, 请退出重新登录!', '系统提示', {
					confirmButtonText: '重新登录',
					callback: (action: Action) => {
						if (action === 'confirm') {
							logout(false, fullPath);
						}
					},
					type: 'warning',
					draggable: true
				});
			}
			return Promise.reject(new Error(res.data.msg));
		}
		//初始密码为123456的 强制用户改密码
		if (res.data?.code === -2) {
			router.push('/resetInitPass')
		}
		// 续期token
		const newToken = res.headers['authorization'];
		if (newToken) {
			setToken(newToken);
		}
		return res;
	},
	(error) => {
		return Promise.reject(error);
	}
);

export default service;